﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.FileProviders;
using PlatformNetCore.Common;
using PlatformNetCore.Common.Helper;
using PlatformNetCore.Common.Utility;
using PlatformNetCore.Entity;
using PlatformNetCore.IService;
using PlatformNetCore.Web.Models;
using System;
using System.IO;

namespace PlatformNetCore.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly IPortalService portalService;
        private readonly IFileProvider fileProvider;
        private readonly IPathProvider pathProvider;
        private readonly IWebHostEnvironment hostingEnvironment;
        public HomeController(IPortalService _portalService, IWebHostEnvironment _hostingEnvironment, IPathProvider _pathProvider)
        {
            portalService = _portalService;
            fileProvider = _hostingEnvironment.ContentRootFileProvider;
            hostingEnvironment = _hostingEnvironment;
            pathProvider = _pathProvider;
        }

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [Route("~/")]
        [Auth("S001")]
        public IActionResult Index()
        {
            try
            {
                return View(portalService.GetHomeInfo());
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        #endregion

        #region 公司介绍
        /// <summary>
        /// 公司介绍
        /// </summary>
        /// <returns></returns>
        [Route("~/about")]
        [Auth("A001")]
        public IActionResult About()
        {
            try
            {
                return View(portalService.GetAboutInfo("A001"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 公司简介
        /// </summary>
        /// <returns></returns>
        [Route("~/about/desc")]
        [Auth("A001")]
        public IActionResult Desc()
        {
            try
            {
                return View("About", portalService.GetAboutInfo("A001"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 企业资质
        /// </summary>
        /// <returns></returns>
        [Route("~/about/qualification")]
        [Auth("A002")]
        public IActionResult Qualification()
        {
            try
            {
                return View(portalService.GetAboutInfo("A002"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 品牌与文化
        /// </summary>
        /// <returns></returns>
        [Route("~/about/brand")]
        [Auth("A003")]
        public IActionResult Brand()
        {
            try
            {
                return View(portalService.GetAboutInfo("A003"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        #endregion

        #region 配药安全
        /// <summary>
        /// 配药安全
        /// </summary>
        /// <returns></returns>
        [Route("~/safety")]
        [Auth("B101")]
        public IActionResult Safety()
        {
            try
            {
                return View(portalService.GetSafetyInfo());
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 输液现状
        /// </summary>
        /// <returns></returns>
        [Route("~/safety/infusion")]
        [Auth("B101")]
        public IActionResult Infusion()
        {
            try
            {
                return View("Safety", portalService.GetSafetyInfo());
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        #endregion

        #region 产品中心
        /// <summary>
        /// 产品中心
        /// </summary>
        /// <returns></returns>
        [Route("~/product")]
        [Auth("C001")]
        public IActionResult Product()
        {
            try
            {
                return View(portalService.GetProductInfo("C001"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 静配中心补充
        /// </summary>
        /// <returns></returns>
        [Route("~/product/static")]
        [Auth("C001")]
        public IActionResult Static()
        {
            try
            {
                return View("Product", portalService.GetProductInfo("C001"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 微型可移动静配中心
        /// </summary>
        /// <returns></returns>
        [Route("~/product/minmove")]
        [Auth("C002")]
        public IActionResult MinMove()
        {
            try
            {
                return View(portalService.GetProductInfo("C002"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 护士安全
        /// </summary>
        /// <returns></returns>
        [Route("~/product/nurse")]
        [Auth("C003")]
        public IActionResult Nurse()
        {
            try
            {
                return View(portalService.GetProductInfo("C003"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 高效配置
        /// </summary>
        /// <returns></returns>
        [Route("~/product/efficient")]
        [Auth("C004")]
        public IActionResult Efficient()
        {
            try
            {
                return View(portalService.GetProductInfo("C004"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        #endregion

        #region 数据中心
        /// <summary>
        /// 数据中心
        /// </summary>
        /// <returns></returns>
        [Route("~/datacenter")]
        [Auth("D001")]
        public IActionResult DataCenter(int pageindex = 1, int pagesize = 4)
        {
            try
            {
                return View(portalService.GetDataCenterInfo("D001", pageindex, pagesize));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 文献
        /// </summary>
        /// <returns></returns>
        [Route("~/datacenter/documents")]
        [Auth("D001")]
        public IActionResult Documents(int pageindex = 1, int pagesize = 4)
        {
            try
            {
                return View("DataCenter", portalService.GetDataCenterInfo("D001", pageindex, pagesize));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 全国医院覆盖区域
        /// </summary>
        /// <returns></returns>
        [Route("~/datacenter/branches")]
        [Auth("D002")]
        public IActionResult Branches(int pageindex = 1, int pagesize = 12)
        {
            try
            {
                return View(portalService.GetDataCenterInfo("D002", pageindex,pagesize));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 专家支持
        /// </summary>
        /// <returns></returns>
        [Route("~/datacenter/support")]
        [Auth("D003")]
        public IActionResult Support(int pageindex = 1, int pagesize = 6)
        {
            try
            {
                return View(portalService.GetDataCenterInfo("D003", pageindex, pagesize));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        #endregion

        #region 招贤纳士
        /// <summary>
        /// 招贤纳士
        /// </summary>
        /// <returns></returns>
        [Route("~/jobs")]
        [Auth("E001")]
        public IActionResult Jobs()
        {
            try
            {
                return View(portalService.GetJobsInfo("E001"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 人才理念
        /// </summary>
        /// <returns></returns>
        [Route("~/jobs/concept")]
        [Auth("E001")]
        public IActionResult Concept()
        {
            try
            {
                return View("Jobs", portalService.GetJobsInfo("E001"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 职位申请
        /// </summary>
        /// <returns></returns>
        [Route("~/jobs/apply")]
        [Auth("E002")]
        public IActionResult Apply(string jobID = "", string positionID = "",string search = "", int pageindex = 1, int pagesize = 8)
        {
            try
            {
                ViewBag.JobID = jobID;
                ViewBag.PositionID = positionID;
                ViewBag.Search = search;
                return View(portalService.GetJobsInfo("E002", jobID, positionID, search, pageindex, pagesize));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        [Route("~/jobs/jobsdetail")]
        [Auth("E002")]
        public IActionResult JobsDetail(string id)
        {
            try
            {
                return View(portalService.GetJobsDetail(id));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        #endregion

        #region 员工专区
        /// <summary>
        /// 员工专区
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("~/employee")]
        [Auth("F001")]
        public IActionResult Employee(int pageindex = 1, int pagesize = 12)
        {
            try
            {
                return View(portalService.GetEmployee("F001",pageindex,pagesize));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 培训系统
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("~/employee/training")]
        [Auth("F001")]
        public IActionResult Training(int pageindex = 1, int pagesize = 12)
        {
            try
            {
                return View("Employee", portalService.GetEmployee("F001", pageindex, pagesize));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 休闲专区
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("~/employee/leisure")]
        [Auth("F002")]
        public IActionResult Leisure(int pageindex = 1, int pagesize = 12)
        {
            try
            {
                return View(portalService.GetEmployee("F002", pageindex, pagesize));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }

        /// <summary>
        /// 休闲专区详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("~/employee/leisuredetail")]
        [Auth("F002")]
        public IActionResult LeisureDetail(string id)
        {
            try
            {
                return View(portalService.GetEmployeeDetail(id));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        #endregion

        #region 客服中心
        /// <summary>
        /// 客服中心
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("~/customer")]
        [Auth("G002")]
        public IActionResult Customer()
        {
            try
            {
                return View(portalService.GetCustomerInfo("G002"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 常见问题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("~/customer/faq")]
        [Auth("G001")]
        public IActionResult FAQ()
        {
            try
            {
                return View(portalService.GetCustomerInfo("G001"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 联系我们
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("~/customer/contact")]
        [Auth("G002")]
        public IActionResult Contact()
        {
            try
            {
                return View("Customer", portalService.GetCustomerInfo("G002"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 留言专区
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("~/customer/comments")]
        [Auth("G003")]
        public IActionResult Comments()
        {
            try
            {
                return View(portalService.GetCustomerInfo("G003"));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Content(CommonConst.SystemErrorMessage);
            }
        }
        /// <summary>
        /// 留言专区
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("~/comment")]
        public IActionResult Comment(Subscribe model)
        {
            try
            {
                return Json(portalService.SubmitComment(model));
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return Json(new { success = false,msg = CommonConst.SystemErrorMessage });
            }
        }
        #endregion

        #region 图片处理
        [Route("/image/{w}/{h}/{*url}")]
        public IActionResult ResizeImage(string url, int w, int h)
        {
            try
            {
                if (w <= 0 || h <= 0)
                {
                    return File(url, "image/*");
                }
                string fileName = url.Replace("/", string.Empty);
                string thumbUrl = $"ThumbCache\\w{w}_h{h}_{fileName}";
                string thumbPath = Path.Combine("\\wwwroot\\", thumbUrl);

                string virualPath = "\\wwwroot\\ThumbCache";
                //保存的物理路径
                string filePath = pathProvider.MapPath(virualPath);
                //没有则创建文件夹
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }

                var thumbInfo = fileProvider.GetFileInfo(thumbPath);
                if (thumbInfo.Exists)
                {
                    string relationPath = $"/ThumbCache/w{w}_h{h}_{fileName}";
                    return File(relationPath, "image/*");
                    //return File(thumbInfo.CreateReadStream(), "image/jpg");
                }
                else
                {
                    string imagePath = url.Replace('/', '\\');
                    string orginPath = $"{hostingEnvironment.WebRootPath}{imagePath}";
                    string outPath = $"{hostingEnvironment.WebRootPath}\\{thumbUrl}";

                    string fileFormat = Path.GetExtension(url).ToLower();
                    ImageHelper.MakeThumbnail(orginPath, outPath, w, h, "Cut", fileFormat);

                    thumbInfo = fileProvider.GetFileInfo(thumbPath);
                    return File(thumbInfo.CreateReadStream(), "image/*");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return File(url, "image/*");
            }
        }
        #endregion
    }
}